// BtnST.cpp : implementation file
//

#include "stdafx.h"
#include "BitmapButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "BtnST.h"

/////////////////////////////////////////////////////////////////////////////
// CBmpButton

CBmpButton::CBmpButton()
{
    m_hIconIn = NULL;
	m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE); 
	m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT); 
	m_crActiveBg = ::GetSysColor(COLOR_BTNFACE); 
	m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT); 
} 


CBmpButton::~CBmpButton()
{
	// Destroy the icons (if any)
	if (m_hIconIn != NULL) ::DeleteObject(m_hIconIn);
}

void CBmpButton::DrawItem(HWND hwnd, LPDRAWITEMSTRUCT lpDIS)
{
  CDC  *pdrawDC = CDC::FromHandle(lpDIS->hDC);
  CMemDC memDC(pdrawDC);
  CDC  *pDC = &memDC;

  CPen *pOldPen;
  BOOL bIsPressed  = (lpDIS->itemState & ODS_SELECTED);
  BOOL bIsFocused  = (lpDIS->itemState & ODS_FOCUS);
  BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
  CRect itemRect = lpDIS->rcItem;
  COLORREF bgColor;

  if (bIsPressed)
		bgColor = m_crActiveBg;
  else
		bgColor = m_crInactiveBg;

  CBrush br(bgColor);
  pDC->FillRect(&itemRect, &br);

  // Draw pressed button
  if (bIsPressed)
  {
	    CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // Bianco
        CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Grigio scuro

        // Disegno i bordi a sinistra e in alto
        // Dark gray line
        pOldPen = pDC->SelectObject(&penBtnShadow);
        pDC->MoveTo(itemRect.left, itemRect.bottom-1);
        pDC->LineTo(itemRect.left, itemRect.top);
        pDC->LineTo(itemRect.right, itemRect.top);
        // Disegno i bordi a destra e in basso
        // White line
        pDC->SelectObject(penBtnHiLight);
        pDC->MoveTo(itemRect.left, itemRect.bottom-1);
        pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
        pDC->LineTo(itemRect.right-1, itemRect.top-1);
        pDC->SelectObject(pOldPen);
  }
  else 
  {
		CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
		CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
		CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
		CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black

  	    // Disegno i bordi a sinistra e in alto
        // White line
        pOldPen = pDC->SelectObject(&penBtnHiLight);
        pDC->MoveTo(itemRect.left, itemRect.bottom-1);
        pDC->LineTo(itemRect.left, itemRect.top);
        pDC->LineTo(itemRect.right, itemRect.top);
        // Disegno i bordi a destra e in basso
        // Dark gray line
        pDC->SelectObject(penBtnShadow);
        pDC->MoveTo(itemRect.left, itemRect.bottom-1);
        pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
        pDC->LineTo(itemRect.right-1, itemRect.top-1);
        pDC->SelectObject(pOldPen);
  }

  CRect captionRect = lpDIS->rcItem;

  // Draw the icon
  if (m_hIconIn != NULL)
  {
	  DrawTheIcon(pDC, NULL, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
  }

} // End of DrawItem


void CBmpButton::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
  CRect iconRect = rcItem;

  // Center the icon horizontally
  iconRect.left += 0;  
  iconRect.top += 0;
    
  if (IsPressed) iconRect.OffsetRect(1, 1);
  pDC->DrawState(iconRect.TopLeft(), iconRect.Size(), m_hIconIn, (IsDisabled ? DSS_DISABLED : DSS_NORMAL), NULL);
}


